package org.dromara.system.service.impl;

import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.AppMessageRemindBo;
import org.dromara.system.domain.vo.AppMessageRemindVo;
import org.dromara.system.domain.AppMessageRemind;
import org.dromara.system.mapper.AppMessageRemindMapper;
import org.dromara.system.service.IAppMessageRemindService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 消息提醒Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppMessageRemindServiceImpl implements IAppMessageRemindService {

    private final AppMessageRemindMapper baseMapper;

    /**
     * 查询消息提醒
     *
     * @param id 主键
     * @return 消息提醒
     */
    @Override
    public AppMessageRemindVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询消息提醒列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 消息提醒分页列表
     */
    @Override
    public TableDataInfo<AppMessageRemindVo> queryPageList(AppMessageRemindBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppMessageRemind> lqw = buildQueryWrapper(bo);
        Page<AppMessageRemindVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的消息提醒列表
     *
     * @param bo 查询条件
     * @return 消息提醒列表
     */
    @Override
    public List<AppMessageRemindVo> queryList(AppMessageRemindBo bo) {
        LambdaQueryWrapper<AppMessageRemind> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppMessageRemind> buildQueryWrapper(AppMessageRemindBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppMessageRemind> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppMessageRemind::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getPlanCode()), AppMessageRemind::getPlanCode, bo.getPlanCode());
        lqw.eq(bo.getRoleId()!=null, AppMessageRemind::getRoleId, bo.getRoleId());
//        lqw.eq(StringUtils.isNotBlank(bo.getRemindUser()), AppMessageRemind::getRemindUser, bo.getRemindUser());
        lqw.eq(bo.getIsRead() != null, AppMessageRemind::getIsRead, bo.getIsRead());
        return lqw;
    }

    /**
     * 新增消息提醒
     *
     * @param bo 消息提醒
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppMessageRemindBo bo) {
        AppMessageRemind add = MapstructUtils.convert(bo, AppMessageRemind.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改消息提醒
     *
     * @param bo 消息提醒
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppMessageRemindBo bo) {
        AppMessageRemind update = MapstructUtils.convert(bo, AppMessageRemind.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppMessageRemind entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除消息提醒信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public int read(Long id) {
        LambdaQueryWrapper<AppMessageRemind> appMessageRemindLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appMessageRemindLambdaQueryWrapper.eq(AppMessageRemind::getId, id);
        AppMessageRemind appMessageRemind = baseMapper.selectOne(appMessageRemindLambdaQueryWrapper);
        if (appMessageRemind != null){
            appMessageRemind.setIsRead(1L);
            return baseMapper.updateById(appMessageRemind);
        }
        return 0;
    }
}
